package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.AppShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppShoppingCartServiceImpl implements AppShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    // 添加购物车
    @Override
    public void save(ShoppingCart shoppingCart) {
        //1. 用户id+菜品id 或者 用户id+套餐id 查询购物车是否存在
        Long userId = UserHolder.get();
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCartDB =shoppingCartMapper.findOne(shoppingCart);
        //2. 如果购物车存在，数量+1，更新数据库
        if(shoppingCartDB != null){
            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
            shoppingCartMapper.update(shoppingCartDB);
        }else{
            //3. 购物车不存在，添加购物车
            if (shoppingCart.getDishId()!=null) { //3-1 菜品
                // 查询菜品表
                DishVO dishVO = dishMapper.findById(shoppingCart.getDishId());
                // 补齐数据
                shoppingCart.setName(dishVO.getName());// 菜名
                shoppingCart.setImage(dishVO.getImage());// 图片
                shoppingCart.setAmount(dishVO.getPrice());// 价格
                shoppingCart.setNumber(1);// 数量
                shoppingCart.setCreateTime(LocalDateTime.now());// 创建时间
                // 保存数据库
                shoppingCartMapper.save(shoppingCart);

            }else{//3-2 套餐
                // 查询套餐表
                SetmealVO setmealVO = setmealMapper.findById(shoppingCart.getSetmealId());
                // 补齐数据
                shoppingCart.setName(setmealVO.getName());// 菜名
                shoppingCart.setImage(setmealVO.getImage());// 图片
                shoppingCart.setAmount(setmealVO.getPrice());// 价格
                shoppingCart.setNumber(1);// 数量
                shoppingCart.setCreateTime(LocalDateTime.now());// 创建时间
                // 保存数据库
                shoppingCartMapper.save(shoppingCart);
            }
        }
    }

    // 查询购物车列表
    @Override
    public List<ShoppingCart> findList() {
        //1. 获取登录人id
        Long userId = UserHolder.get();
        //2. 调用mapper查询并返回
        return shoppingCartMapper.findByUserId(userId);
    }

    // 清空购物车
    @Override
    public void clean() {
        //1. 获取登录人id
        Long userId = UserHolder.get();
        //2. 根据用户id删除购物车
        shoppingCartMapper.deleteByUserId(userId);
    }

    //删减购物车
    @Override
    public void sub(ShoppingCart shoppingCart) {
        //查询购物车
        shoppingCart.setUserId(UserHolder.get());
        ShoppingCart shoppingCartDB = shoppingCartMapper.findOne(shoppingCart);
        //如果数量大于1，菜品或套餐数量-1
        if (shoppingCartDB.getNumber() > 1) {
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() - 1);
            shoppingCartMapper.update(shoppingCartDB);
        } else {//如果数量等于1，删除该菜品或套餐
            shoppingCartMapper.deleteById(shoppingCartDB.getId());
        }
    }
}
